package com.wql.controller;

import com.aaa.entity.BAppeal;
import com.aaa.entity.BBusinessInfo;

import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wql.mapper.BBusinessInfoMapper;
import com.wql.service.BBusinessInfoService;
import com.wql.service.OStockService;
import com.wql.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author wql
 * @version 1.0
 * @date 2023/3/3 16:21
 */
@RestController
@RequestMapping("/business")
public class BusinessMsgController {

    @Autowired
    private BBusinessInfoService bBusinessInfoService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private BBusinessInfoMapper bBusinessInfoMapper;

    @GetMapping("/showshop")
    public Result showshop() {
        return bBusinessInfoService.getBusinessInfo();
    }

    @PostMapping("/changeshop")
    public Result changeshop(@RequestBody BBusinessInfo businessInfo) {
        return bBusinessInfoService.changeshop(businessInfo);
    }

    @PostMapping("/changepass/{password}/{code}")
    public Result changepass(@PathVariable String password, @PathVariable String code) {
        String password1 = passwordEncoder.encode(password);
        HttpServletRequest request = WebUtils.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        Object username = tokenChaim.get("username");

        String telephone = username.toString();
        return bBusinessInfoService.changePassword(telephone, password, code);
    }

    //验证码
    @GetMapping("/noteByPhone/{telephone}")
    public Result noteByPhone(@PathVariable String telephone) {
        return bBusinessInfoService.getMessage(telephone);
    }

    @PostMapping("/getsession")
    public Result getsession() {
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        return new Result(2000, "成功", tokenChaim.get("loginType"));
    }

    @RequestMapping("/upload01")
    public Result updateResult(@RequestBody MultipartFile file) {
        return bBusinessInfoService.upload(file);
    }



    @PostMapping("/storeinsert")
    public Result storeinsert(@RequestBody BBusinessInfo businessInfo) {

        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String token1 = (String) tokenChaim.get("username");
        QueryWrapper<BBusinessInfo> Wrapper = new QueryWrapper<>();
        Wrapper.eq("telephone", token1);
        BBusinessInfo businessInfo1 = bBusinessInfoMapper.selectOne(Wrapper);
        if (businessInfo1==null){
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String password = businessInfo.getPassword();
            System.out.println("password = " + password);
            String encode = bCryptPasswordEncoder.encode(password);
            businessInfo.setPassword(encode);
            Result insert = bBusinessInfoService.insert(businessInfo);
            return new Result<>(2000,"添加成功",insert);
        }else if (businessInfo1.getStatus()==1){
            return new Result(4000,"您已经被驳回了，请在帮助文档联系管理员");
        }else if (businessInfo1.getStatus() == 2){
            return new Result(2000,"请等待1-2日审核");
        }
            return new Result(4000,"您已经是商家了");

    }

    @GetMapping("/listSort")
    public Result listSort(){
        Result sortname = bBusinessInfoService.sortname();
        return sortname;

    }
    @GetMapping("/username")
    public Result username(){
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        Object usernmae = tokenChaim.get("username");
        return new Result(2000, "成功", usernmae);
    }

    @PostMapping("/appeal/shangjiashensu")
    public Result shangjiashensu(@RequestBody BAppeal bAppeal){
            return  bBusinessInfoService.shangjiashensu(bAppeal);
    }

}
